package com.xls.dormitoryservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.exception.BusinessException;
import com.xls.dormitoryservice.entity.Hygiene;
import com.xls.dormitoryservice.entity.Room;
import com.xls.base.model.Student;
import com.xls.dormitoryservice.mapper.HygieneMapper;
import com.xls.dormitoryservice.mapper.RoomMapper;
import com.xls.base.mapper.StudentMapper;
import com.xls.dormitoryservice.service.HygieneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xls.dormitoryservice.vo.HygieneQuery;
import com.xls.dormitoryservice.vo.HygieneVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@Service
public class HygieneServiceImpl extends ServiceImpl<HygieneMapper, Hygiene> implements HygieneService {

    @Autowired
    private HygieneMapper hygieneMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public HygieneVo getHygiene(String id) {
        return hygieneMapper.getHygiene(id);
    }

    @Override
    public HygieneVo saveHygiene(HygieneVo hygieneVo) {
        Hygiene hygiene = new Hygiene();
        BeanUtils.copyProperties(hygieneVo, hygiene);
        validateHygiene(hygieneVo, hygiene);
        this.save(hygiene);
        return hygieneMapper.getHygiene(hygiene.getId().toString());
    }

    @Override
    public HygieneVo updateHygiene(HygieneVo hygieneVo) {
        Hygiene hygiene = new Hygiene();
        BeanUtils.copyProperties(hygieneVo, hygiene);
        validateHygiene(hygieneVo, hygiene);
        this.updateById(hygiene);
        return hygieneMapper.getHygiene(hygiene.getId().toString());
    }

    private void validateHygiene(HygieneVo hygieneVo, Hygiene hygiene) {
        Room room = roomMapper.selectOne(new QueryWrapper<Room>()
                .eq("building", hygieneVo.getBuilding())
                .eq("floor", hygieneVo.getFloor())
                .eq("rno", hygieneVo.getRno()));
        if (room == null) {
            throw new BusinessException(77777, "没有此宿舍");
        } else {
            hygiene.setRoomId(room.getId());
        }
        Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("sno", hygieneVo.getSno()));
        if (student == null) {
            throw new BusinessException(77777, "没有学号为" + hygieneVo.getSno() + "的学生");
        } else {
            hygiene.setStudentId(student.getId());
        }
    }

    @Override
    public Page<HygieneVo> getHygieneVoPage(Integer page, Integer limit, HygieneQuery hygieneQuery) {
        QueryWrapper<Hygiene> hygieneQueryWrapper = new QueryWrapper<>();
        String building = hygieneQuery.getBuilding();
        String floor = hygieneQuery.getFloor();
        String rno = hygieneQuery.getRno();
        Integer grade = hygieneQuery.getGrade();
        String studentId = hygieneQuery.getStudentId();
        String sno = hygieneQuery.getSno();
        String name = hygieneQuery.getName();
        String begin = hygieneQuery.getBegin();
        String end = hygieneQuery.getEnd();
        hygieneQueryWrapper
                .eq(!StringUtils.isEmpty(building), "r.building", building)
                .eq(!StringUtils.isEmpty(floor), "r.floor", floor)
                .likeRight(!StringUtils.isEmpty(rno), "r.rno", rno)
                .eq(!StringUtils.isEmpty(grade), "h.grade", grade)
                .eq(!StringUtils.isEmpty(studentId), "h.student_id", studentId)
                .likeRight(!StringUtils.isEmpty(sno), "s.sno", sno)
                .likeRight(!StringUtils.isEmpty(name), "s.name", name)
                .gt(!StringUtils.isEmpty(begin), "h.check_time", begin)
                .lt(!StringUtils.isEmpty(end), "h.check_time", end)
                .eq("h.is_deleted", 0);
        return hygieneMapper.getHygieneVoPage(new Page<>(page, limit), hygieneQueryWrapper);
    }
}
